package de.duester.genealogy.adapter;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import de.duester.genealogy.R;
import de.duester.genealogy.model.EventCode;

public class EventTypeListAdapter extends ArrayAdapter<EventCode> implements
		Comparator<EventCode> {
	private Activity activity;
	private List<EventCode> eventCodes;

	public EventTypeListAdapter(Context context, int textViewResourceId,
			List<EventCode> eventCodes) {
		super(context, textViewResourceId, eventCodes);
		activity = (Activity) context;
		this.eventCodes = eventCodes;
		Collections.sort(this.eventCodes, this);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v;

		if (convertView == null) {
			LayoutInflater li = activity.getLayoutInflater();
			v = li.inflate(R.layout.list_element, null);
		} else
			v = convertView;

		TextView tvText = (TextView) v.findViewById(R.id.tvText);
		tvText.setText(eventCodes.get(position).getType());

		v.setBackgroundResource(R.drawable.list_element);

		return v;
	}

	@Override
	public EventCode getItem(int position) {
		return eventCodes.get(position);
	}

	@Override
	public int compare(EventCode lhs, EventCode rhs) {
		return lhs.getType().compareTo(rhs.getType());
	}

}
